package com.xuxin.baby.fragment.teach;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.xuxin.baby.R;
import com.xuxin.baby.activity.ReaderActivity;
import com.xuxin.baby.activity.ReaderVideoSlidingActivity;
import com.xuxin.baby.activity.VideoActivity;
import com.xuxin.baby.adapter.MenuCourseAdapter;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.base.BaseFragment;
import com.xuxin.baby.bean.ReaderVideoBean;
import com.xuxin.baby.contract.ReaderVideoContract;
import com.xuxin.baby.presenter.ReaderVideoPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;

import butterknife.BindView;

/**
 * Created by xu_xin
 * Created on 2020/12/22 0022
 * 精婴早教 -> 课程
 */
public class TeachCourseFragment extends BaseFragment implements ReaderVideoContract.View {
    private final ReaderVideoContract.Presenter presenter = new ReaderVideoPresenter(this);
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;
    @BindView(R.id.smart_recycle)
    RecyclerView smart_recycle;

    private String tenantId;//租户id
    private String token;//授权token
    private MenuCourseAdapter menuCourseAdapter;
    private String title, id;

    public static TeachCourseFragment newInstance(String title, String id) {
        TeachCourseFragment fragment = new TeachCourseFragment();
        Bundle bundle = new Bundle();
        bundle.putString("id", id);
        bundle.putString("title", title);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View setContentView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        return inflater.inflate(R.layout.fragment_smart, container, false);
    }

    @Override
    public void initView() {
        smart_refresh.setEnableRefresh(false);
        smart_refresh.setEnableLoadMore(false);
        menuCourseAdapter = new MenuCourseAdapter();
        RecycleUtils.initNested(smart_recycle);
        smart_recycle.setAdapter(menuCourseAdapter);
        View more = View.inflate(context, R.layout.view_more, null);
        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    if (!StringUtils.isEmpty(title) && !StringUtils.isEmpty(id)) {
                        Bundle bundle = new Bundle();
                        bundle.putString("title", title);
                        bundle.putString("categoryId", id);
                        ActivityUtils.startActivity(bundle, ReaderVideoSlidingActivity.class);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        menuCourseAdapter.addFooterView(more);
        menuCourseAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                try {
                    int cType = menuCourseAdapter.getData().get(position).getItemType();
                    if (0 == cType) {
                        Bundle bundle = new Bundle();
                        bundle.putString(BaseConstant.videoId, menuCourseAdapter.getData().get(position).getId());
                        ActivityUtils.startActivity(bundle, VideoActivity.class);
                    } else {
                        Bundle bundle2 = new Bundle();
                        bundle2.putString(BaseConstant.readerId, menuCourseAdapter.getData().get(position).getId());
                        ActivityUtils.startActivity(bundle2, ReaderActivity.class);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(getContext());
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        if (null != getArguments()) {
            title = getArguments().getString("title");
            id = getArguments().getString("id");
        }
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onSuccess(ReaderVideoBean readerVideoBean) {
        menuCourseAdapter.setList(readerVideoBean.getResult().getItems());
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 0:
                    LogUtils.e("MenuCourseFragment id = " + id);
                    presenter.getReaderVideo(tenantId, token, id, null, null, "video", null, 0);
                    break;
            }
            return false;
        }
    });

    private boolean notFirst;

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            LogUtils.e(id + "TeachCourseFragment当前处于显示");
            if (!notFirst) {
                LogUtils.e(id + "TeachCourseFragment当前为第一次");
                notFirst = true;
                menuCourseAdapter.setList(null);
                //popup.setTitle("加载中").show();
                handler.sendEmptyMessage(0);
            } else {
                LogUtils.e(id + "TeachCourseFragment当前不是第一次");
            }
        } else {
            LogUtils.e(id + "TeachCourseFragment当前处于隐藏");
        }
    }
}